﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Linq;

namespace Lincolnwood.Foundation.Common.Data.NHibernate
{
    public abstract class NHibernateReferenceDataRepositoryBase<TAggregateRoot, TKey> : IReadableRepository<TAggregateRoot, TKey> 
        where TAggregateRoot : EntityBase<TKey>
        where TKey : struct
    {
        private NHibernateUnitOfWork _unitOfWork;

        protected NHibernateReferenceDataRepositoryBase(NHibernateUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        protected NHibernateUnitOfWork UnitOfWork
        {
            get { return _unitOfWork; }
        }

      
        public TAggregateRoot FindById(TKey id)
        {
            return _unitOfWork.Session.Get<TAggregateRoot>(id);
        }

        public int GetCount()
        {
            int count = _unitOfWork.Session.Query<TAggregateRoot>().Count();
            return count;
        }

        public IEnumerable<TAggregateRoot> All()
        {
            var query = _unitOfWork.Session.Query<TAggregateRoot>();
            return query.ToList<TAggregateRoot>();
        }
    }
}
